package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func postForm() {
	// form表单提交 和 get请求中的提交参数类似
	data := make(url.Values)
	data.Add("name", "poloxue")
	data.Add("age", "18")
	payload := data.Encode()
	r, _ := http.Post(
		"http://httpbin.org/post",
		"application/x-www-form-urlencoded",
		strings.NewReader(payload),
	)
	defer r.Body.Close()
	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func Struct2Map(data interface{}) (map[string]string, error) {
	out := make(map[string]string)
	temp, err := json.Marshal(data)
	if err != nil {
		return out, err
	}
	if err := json.Unmarshal(temp, &out); err != nil {
		return out, err
	}
	return out, nil
}

func postForm2() {
	u := struct {
		Name string `json:"name"`
		Age  string `json:"age"`
	}{
		Name: "hzw",
		Age:  "19",
	}
	m, _ := Struct2Map(u)
	data := make(url.Values)
	for k, v := range m {
		data.Add(k, v)
	}
	payload := data.Encode()
	r, _ := http.NewRequest("POST", "http://httpbin.org/post", bytes.NewBuffer([]byte(payload)))
	r.Header.Set("content-type", "application/x-www-form-urlencoded")
	res, _ := http.DefaultClient.Do(r)
	defer res.Body.Close()
	content, _ := ioutil.ReadAll(res.Body)
	fmt.Printf("%s", content)
}

func postJson() {
	u := struct {
		Name string `json:"name"`
		Age  int    `json:"age"`
	}{
		Name: "hzw",
		Age:  19,
	}
	payload, _ := json.Marshal(u)
	r, _ := http.Post(
		"http://httpbin.org/post",
		"application/json",
		bytes.NewReader(payload),
	)
	defer r.Body.Close()
	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func postJson2(u interface{}) {
	payload, _ := json.Marshal(u)
	payload1 := string(payload)
	request, _ := http.NewRequest("POST", "http://httpbin.org/post", bytes.NewBuffer([]byte(payload1)))
	r, _ := http.DefaultClient.Do(request)
	defer r.Body.Close()
	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func postFile() {
	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	_ = writer.WriteField("words", "123")
	// defer writer.Close()
	upload1Writer, _ := writer.CreateFormFile("uploadfile1", "uploadfile1")
	uploadFile1, _ := os.Open("uploadfile1")
	defer uploadFile1.Close()
	_, _ = io.Copy(upload1Writer, uploadFile1)

	upload2Writer, _ := writer.CreateFormFile("uploadfile2", "uploadfile2")
	uploadFile2, _ := os.Open("uploadfile2")
	defer uploadFile2.Close()
	_, _ = io.Copy(upload2Writer, uploadFile2)
	writer.Close()
	// fmt.Println(writer.FormDataContentType())
	// fmt.Println(body.String())

	r, _ := http.Post("http://httpbin.org/post", writer.FormDataContentType(), body)
	defer r.Body.Close()
	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func main() {
	// post 请求 本质是request body提交；相对与get请求（urlencoded提交参数，大小限制2kb）

	// post form
	// postForm()
	// postForm2()

	// post json
	// postJson()
	// 测试
	// postJson2(struct {
	// 	Name string `json:"name"`
	// 	Age  int    `json:"age"`
	// }{
	// 	Name: "hzw",
	// 	Age:  19,
	// })

	// post 文件
	postFile()
}
